<template>
	<u-navbar title="资助政策" left-icon-color="white" :placeholder="true" title-style="color:white" bgColor="#359FE3"
		:autoBack="true"></u-navbar>
	<view class="content">

		<scroll-view scroll-y="true" class="scroll-Y">
			<view class="p-3">
				<u-parse :content="zhengce.cotent"></u-parse>
			</view>
			<button type="primary" v-if="isread" :disabled="readFlag" @click="alreadyRead">我已阅读{{showTime}}</button>
			<button type="primary" v-if="!isread"  :disabled="readFlag" @click="read">我已阅读{{showTime}}</button>
		</scroll-view>

	</view>
</template>

<script>
	import {
		queryZhengce,
		readZhengce
	} from '@/api/login.js'

	import lodashFull from '@/uni_modules/iRainna-lodash/js_sdk/js/lodash.full.min.js'
	const {
		isNil
	} = lodashFull()

	export default {
		data() {
			return {
				stuId: '',
				activityId:'',
				student: {},
				readFlag: true,
				zhengceReadRecord:{},
				isread:false,
				time: 10,
				interval: {},
				zhengce: {}
			}
		},
		onLoad(option) {
			
			this.stuId =getApp().globalData.studentId
			this.activityId =getApp().globalData.activityId
			
			queryZhengce(this.stuId,this.activityId ).then(res => {
				if (res.success) {
					this.zhengceReadRecord = res.zhengceReadRecord
					this.isread = res.zhengceReadRecord!=undefined&& res.zhengceReadRecord!=null
					this.zhengce = res.zhengce
				}
			})
			this.interval = setInterval(() => {
				if (this.time > 0) {
					this.time = this.time - 1
				}
			}, 1000)
			this.interval = setTimeout(() => {
				this.readFlag = false
			}, this.time * 1000)
		},
		computed: {
			showTime() {
				return this.time > 0 ? '(' + this.time + ')' : ''
			}
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			
			
			confirmShenqing() {
			  return new Promise((resolve, reject) => {
			    uni.showModal({
			      title: '提示',
				  confirmText: '去申请',
				  cancelText: '放弃申请',
			      content: '是否要申请资助?',
			      success: (res) => {
			        if (res.confirm) {
			          resolve(true);
			        } else if (res.cancel) {
			          resolve(false);
			        }
			      },
			      fail: reject
			    });
			  });
			},
			alreadyRead(){
				readZhengce(this.stuId, this.zhengce.id,this.activityId, 0).then(res => {
						if (res.success) {
							uni.redirectTo({
								url: '/pages/index/index'
							})
						}
					})
			},
			
			read() {
				let that = this
				this.confirmShenqing().then((flag)=>{
					if(flag){
						readZhengce(that.stuId, that.zhengce.id,that.activityId, 0).then(res => {
							if (res.success) {
								uni.redirectTo({
									url: '/pages/shenqingForm/shenqingForm?studentId=' +
										that.stuId
								})
							}
						})
					}else{
						uni.showModal({
							title: '警告',
							confirmText: '确定',
							cancelText: '取消',
							content: '您确定要放弃申请吗？（放弃申请后，本次资助活动将无法再次发起申请）',
							success: function(res) {
								if (res.confirm) {
									readZhengce(that.stuId, that.zhengce.id,that.activityId, 1)
										.then(res => {
											if (res.success) {
												uni.redirectTo({
													url: '/pages/index/index'
												})
											}
										})
								}
							},
							fail(res) {
								console.log(res);
							}
						});
					}
				})

			}
		}
	}
</script>

<style>
	.nav-title {
		font-size: 40rpx;
		width: 100%;
		text-align: center;
	}

	.active {
		border-color: #359fe3;
	}

	.content {
		padding: 40rpx;
	}
</style>